package com.mika.music.mapper;

import com.mika.music.model.Music;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface MusicMapper {

    Integer insert(Music music);

    @Select("select * from music where delete_flag = 0 and title = #{title}")
    Music getSongByName(String title);

    @Select("select * from music where title = #{title}")
    Music getSongByName2(String title);

    @Update("update music set delete_flag = 0 , user_id = #{userId} where id = #{id}")
    Integer updateDeleteFlag(Integer id, Integer userId);

    @Delete("delete from music where id = #{id}")
    Integer deleteById(Integer id);

    Integer deleteByIds(List<Integer> ids);

    @Select("select * from music where delete_flag = 0")
    List<Music> getMusics();

    @Select("select * from music where title like concat('%', #{title}, '%') and delete_flag = 0")
    List<Music> getMusicsByName(String title);

    @Select("select * from music where id = #{id}")
    Music getMusicById(Integer id);


    List<Music> getMusicsByIds(List<Integer> ids);

    // 获取用户最近收听的 100 首音乐详情
    @Select("select m.* from listen as l, music as m where l.user_id = #{userId} and l.music_id = m.id group by m.id order by l.create_time desc limit 100")
    List<Music> getLast100ListenedMusic(Integer userId);


    // 根据特定音乐获取高播放量的音乐
    @Select("select count(l.music_id) as times, m.* from music_label as ml, listen as l, music as m where ml.label_name = #{labelName} and l.music_id = ml.music_id and ml.music_id = m.id group by l.music_id order by times desc")
    List<Music> getHighViewCountMusicByLabelName(String labelName);


    List<Music> getMusicsNotInMusicIds(List<Integer> ids);

    List<Music> getMusicsNotInMusicIdsAndTitleLike(List<Integer> ids, String title);
}
